<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-page-header
            style="background-color: #0096dc;color: white;line-height: 60px"
            @back="goBack()" content="修改商品页面"></el-page-header>
    <el-card style="width: 600px;height: 800px;margin: 0 auto">
        <el-form label-width="80px">
            <el-form-item label="商品标题">
                <el-input v-model="p.title" type="text"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
                <el-input v-model="p.price" type="text"></el-input>
            </el-form-item>
            <el-form-item label="商品内容">
                <el-input v-model="p.content" type="text"></el-input>
            </el-form-item>
            <el-form-item label="商品图片">
                <img :src="p.url" width="100">
                <!--上传组件开始-->
                <el-upload
                        action="/upload"
                        name="picFile"
                        limit="1"
                        orderList-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-success="handleSuccess"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
                <!--上传组件结束-->
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="update()">修改商品</el-button>
            </el-form-item>
        </el-form>
    </el-card>


</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                categoryArr: [],
                p: {title: "", price: "", content: "", url: ""},
            }
        },
        methods: {
            update() {
                if (v.p.url == "") {
                    v.$message.error("请选择上传的图片!");
                    return;
                }
                axios.post("/product/update", v.p).then(function () {
                    location.href = "/admin.html";
                })
            },
            handleSuccess(response, file, fileList) {
                v.p.url = response;
            },
            goBack() {
                history.back();//返回上一页面
            },
            handleRemove(file, fileList) {
                v.p.url = "";
                axios.post("/remove?url=" + file.response).then(function () {
                    console.log("服务器文件删除完成!");
                })
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        },
        created: function () {
            axios.get("/product/selectById" + location.search).then(function (response) {
                v.p = response.data;
            })
        }
    })
</script>
</html>